package com.koron.standard.riskSources.controller;


import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koron.bean.base.Response;
import com.koron.bean.util.RedisUtils;
import com.koron.standard.bean.query.PageQuery;
import com.koron.standard.riskSources.bean.dto.RiskExcel;
import com.koron.standard.riskSources.bean.po.RiskSources;
import com.koron.standard.riskSources.bean.query.RiskSourcesQuery;
import com.koron.standard.riskSources.excel.RiskSourcesListener;
import com.koron.standard.riskSources.service.RiskItemService;
import com.koron.standard.riskSources.service.RiskSourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * 作业风险源(RiskSources)表控制层
 *
 * @author jzp
 * @since 2021-09-10
 */
@Api(tags = "作业风险源接口")
@RestController
@RequestMapping("riskSources")
public class RiskSourcesController {

    @Autowired
    private RiskSourcesService riskSourcesService;

    @Autowired
    private RiskItemService riskItemService;

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<Page<RiskSources>> page(PageQuery<RiskSources> pageQuery, RiskSourcesQuery query) {
        Page<RiskSources> page = riskSourcesService.page(pageQuery, query);
        return Response.succ(page, page.getTotal());
    }

    @ApiOperation(value = "根据父id查询等级分类信息", notes = "为0表示查询一级分类，传入firstTypeName则查询二级分类")
    @GetMapping("types")
    public Response<List<RiskSources>> listByParentId(String firstName) {
        List<RiskSources> riskSources = riskSourcesService.listByParentId(firstName);
        return Response.ok(riskSources);
    }

    @ApiOperation(value = "获取同步时间")
    @GetMapping("syncTime")
    public Response getSyncTime() {
        return Response.ok(RedisUtils.get("risk_source_sync_time"));
    }

    @ApiOperation(value = "手动同步")
    @PutMapping("sync")
    public Response sync() {
        riskSourcesService.sync();
        return Response.ok("同步成功");
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public Response save(@RequestBody RiskSources riskSource) {
        riskSourcesService.save(riskSource);
        return Response.ok(riskSource.getId());
    }

    @ApiOperation(value = "修改")
    @PutMapping
    public Response update(@RequestBody RiskSources riskSource) {
        riskSourcesService.updateById(riskSource);
        return Response.ok();
    }

    @ApiOperation(value = "删除", notes = "会同时删除风险项数据")
    @DeleteMapping
    public Response delete(@RequestParam String id) {
        riskSourcesService.removeRiskById(id);
        return Response.ok();
    }

    @SneakyThrows
    @ApiOperation(value = "下载模板")
    @GetMapping("template")
    public void template(HttpServletResponse response) {
        String path = "excel/risk_standard.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        Workbook book = WorkbookUtil.createBook(classPathResource.getInputStream());
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setHeader("Content-Disposition","attachment;filename=作业风险库.xlsx");
        book.write(response.getOutputStream());
    }

    @SneakyThrows
    @ApiOperation(value = "导入")
    @PostMapping("import")
    public Response importFile(@RequestParam("file") MultipartFile file) {
        EasyExcel.read(file.getInputStream(), new RiskSourcesListener(riskSourcesService, riskItemService))
                .sheet("作业风险库").doRead();
        return Response.ok("导入成功", null);
    }

    @SneakyThrows
    @ApiOperation(value = "导出", notes = "根据条件导出")
    @GetMapping("export")
    public void exportFile(HttpServletResponse response, RiskSourcesQuery query) {
        String path = "excel/risk_standard_export.xlsx";
        ClassPathResource classPathResource = new ClassPathResource(path);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("作业风险库", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<RiskExcel> riskExcelList = riskSourcesService.listExcel(query);
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                .sheet("作业风险库")
                .doFill(riskExcelList);
    }

}
